unit person;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  hadeobject,
  office,
  contact;
Type

  { TPerson }

  TPerson = class(THadeObject)
  private
    FAge: integer;
    FLName: string;
    FName: string;
    fOffice: TOffice;
    fContactList: TContactList;
    function getContactList: TContactList;
    function getOffice: TOffice;
  public
    destructor destroy;override;

    property Contact : TContactList read getContactList;
    function FullName:string;
  published
    property First_Name: string read FName write FName;
    property Last_Name: string read FLName write FLName;
    property Age: integer read FAge write FAge;
    property Office : TOffice read getOffice write fOffice;
  end;

  { TPersonList }

  TPersonList = class(THadeObjectList)
  private
  protected
    function getItem(index: integer): TPerson;Reintroduce;
  public
    property Items[index:integer] :TPerson read getItem;

    //procedure getAll();
    //procedure getFirst();

    constructor create;
  end;

  procedure RegisterMapping;
implementation

uses
  hademapping,
  hadecriteria;

procedure RegisterMapping;
begin
  GHadeMappingManager.RegisterMapping('test',TPerson, 'person', 'OID', 'oid',
    ftInteger, 0, [ptkPK, ptkReadOnly]);

  GHadeMappingManager.RegisterMapping('test',TPerson, 'person', 'First_Name',
    'first_name', ftVarchar, 25);

  GHadeMappingManager.RegisterMapping('test',TPerson, 'person', 'Age', 'age', ftInteger,3);

  GHadeMappingManager.RegisterMapping('test',TPerson, 'person', 'Last_Name',
    'last_name', ftVarchar, 25);

  GHadeMappingManager.RegisterMapping('test',TPerson, 'person', 'Office',
    'oidoffice', ftReference,0,[ptkFK]);

  GHadeMappingManager.RegisterObjectList('test',TPersonList,TPerson);
end;

{ TPersonList }

function TPersonList.getItem(index: integer): TPerson;
begin
  Result := TPerson(inherited getItem(index));
end;

constructor TPersonList.create;
begin
  inherited create(nil);
end;

{ TPerson }

function TPerson.getOffice: TOffice;
begin
  //if fOffice.objectState = posPK then
  //  asession.read(fOffice,False);
  Result:=fOffice;
end;

function TPerson.getContactList: TContactList;
begin
  if not Assigned(fContactList) then
  begin
    fContactList:=TContactList.Create();
    //fContactList.getContactByOIDPerson(self.OID.IntegerOID);
  end;
  Result:=fContactList;
end;

destructor TPerson.destroy;
begin
  FreeAndNil(fOffice);
  if Assigned(fContactList) then
    FreeAndNil(fContactList);
  inherited destroy;
end;

function TPerson.FullName: string;
begin
  Result:=fName+' '+fLName;
end;

end.

